#include "VSPhoneShaderFunction.h"
#include "VSShaderStringFactory.h"
#include "VSGraphicInclude.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSPhoneShaderFunction,VSShaderMainFunction)
BEGIN_ADD_PROPERTY(VSPhoneShaderFunction,VSShaderMainFunction)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSPhoneShaderFunction)
IMPLEMENT_INITIAL_END
VSPhoneShaderFunction::VSPhoneShaderFunction(const VSUsedName &ShowName,VSMaterial * pMaterial)
:VSShaderMainFunction(ShowName,pMaterial)
{

	
}
bool VSPhoneShaderFunction::GetFuntionString(VSString &OutString)const
{
	if(!m_LightArray.GetNum() || 
		(!m_pInput[IN_DIFFUSE_COLOR]->GetOutputLink() && !m_pInput[IN_SPECULAR_COLOR]->GetOutputLink()))
	{
		OutString +=  m_pOutput[OUT_COLOR]->GetNodeName().GetString() + _T(" = ") + m_pInput[IN_DIFFUSE_COLOR]->GetNodeName().GetString() + _T(" + ") +
			m_pInput[IN_EMISSIVE_COLOR]->GetNodeName().GetString() +_T(";\n");


	}
	else
	{

		int iLightNum[VSLight::LT_MAX] = { 0 };	
		for (unsigned int i = 0 ; i < m_LightArray.GetNum() ; i++)
		{
			if (m_LightArray[i])
			{
				for (unsigned int j = 0 ; j < VSLight::LT_MAX ; j++)
				{
					if (m_LightArray[i]->GetLightType() == j)
					{

						iLightNum[j]++;
					}
				}
			}
		}

		if(VSShaderStringFactory::ms_PSInputLocalNormal.GetLength() == 0)
		{
			OutString += VSShaderStringFactory::ms_WorldNormal + _T(" = ") + VSRenderer::ms_pRenderer->Float3Const(_T("0"),_T("0"),_T("0")) + _T(";\n");
		}
		else if(!m_pInput[IN_NORMAL]->GetOutputLink() || VSShaderStringFactory::ms_PSInputLocalTangent.GetLength() == 0)
		{

			OutString += VSShaderStringFactory::ms_WorldNormal + _T(" = ");
			VSRenderer::ms_pRenderer->LocalToWorldNormal(VSShaderStringFactory::ms_PSInputLocalNormal,OutString);
			OutString += _T(";\n");

		}
		else
		{

			OutString += VSRenderer::ms_pRenderer->Float3() + _T("LocalNormal = ");
			VSRenderer::ms_pRenderer->BumpNormal(m_pInput[IN_NORMAL]->GetNodeName().GetString(),OutString); 
			OutString += _T(";\n");


			OutString += VSShaderStringFactory::ms_WorldNormal + _T(" = ");
			VSRenderer::ms_pRenderer->LocalToWorldNormal(_T("LocalNormal"),OutString);
			OutString += _T(";\n");
		}

		OutString += VSShaderStringFactory::ms_WorldPos + _T(" = ");
		VSRenderer::ms_pRenderer->LocalToWorldPos(VSShaderStringFactory::ms_PSInputLocalPos,OutString);
		OutString += _T(";\n");


		OutString += VSShaderStringFactory::ms_WorldCameraDir + _T(" = ");
		VSRenderer::ms_pRenderer->ComputeDir(VSShaderStringFactory::ms_CameraWorldPos,VSShaderStringFactory::ms_WorldPos,OutString);
		OutString += _T(";\n");

		OutString +=  m_pOutput[OUT_COLOR]->GetNodeName().GetString() + _T(" = ") +
			m_pInput[IN_EMISSIVE_COLOR]->GetNodeName().GetString(); 
		for (unsigned int i = 0 ; i < VSLight::LT_MAX ; i++)
		{
			if (!iLightNum[i])
			{
				continue;
			}
			if(i == VSLight::LT_DIRECTION)
			{			


				VSRenderer::ms_pRenderer->DirectionalLight(iLightNum[i],m_pInput[IN_DIFFUSE_COLOR]->GetNodeName().GetString()
					,m_pInput[IN_SPECULAR_COLOR]->GetNodeName().GetString(),m_pInput[IN_SPECULAR_POW]->GetNodeName().GetString(),VSShaderStringFactory::ms_WorldNormal,VSShaderStringFactory::ms_WorldCameraDir,OutString);


			}
			else if(i == VSLight::LT_POINT)
			{


				VSRenderer::ms_pRenderer->PointLight(iLightNum[i],m_pInput[IN_DIFFUSE_COLOR]->GetNodeName().GetString()
					,m_pInput[IN_SPECULAR_COLOR]->GetNodeName().GetString(),m_pInput[IN_SPECULAR_POW]->GetNodeName().GetString(),VSShaderStringFactory::ms_WorldPos,VSShaderStringFactory::ms_WorldNormal,VSShaderStringFactory::ms_WorldCameraDir,OutString);

			}
			else if(i == VSLight::LT_SPOT)
			{

				VSRenderer::ms_pRenderer->SpotLight(iLightNum[i],m_pInput[IN_DIFFUSE_COLOR]->GetNodeName().GetString()
					,m_pInput[IN_SPECULAR_COLOR]->GetNodeName().GetString(),m_pInput[IN_SPECULAR_POW]->GetNodeName().GetString(),VSShaderStringFactory::ms_WorldPos,VSShaderStringFactory::ms_WorldNormal,VSShaderStringFactory::ms_WorldCameraDir,OutString);

			}

		}
		OutString += _T(";\n");
	}

	if (m_uiVESRGBWrite)
	{
		VSString NodeString = VSRenderer::ms_pRenderer->GetValueElement(m_pOutput[OUT_COLOR],m_uiVESRGBWrite);

		OutString += NodeString + _T(" = ");
		VSRenderer::ms_pRenderer->GreaterZeroPow(NodeString,1.0f / 2.2f,OutString);
		OutString += _T(";\n");
	}

	VSString NodeStringA = VSRenderer::ms_pRenderer->GetValueElement(m_pOutput[OUT_COLOR],VSRenderer::VE_A);
	OutString += NodeStringA + _T(" = ") + m_pInput[IN_ALPHA]->GetNodeName().GetString();
	OutString += _T(";\n");
	OutString +=  VSShaderStringFactory::ms_PSTextureOutputColorValue + _T(" = ") + m_pOutput[OUT_COLOR]->GetNodeName().GetString() + _T(";\n");
	return 1;

}
VSPhoneShaderFunction::~VSPhoneShaderFunction()
{

}
VSPhoneShaderFunction::VSPhoneShaderFunction()
{
	
}